//
//  LoginViewController.m
//  XDCommonApp
//
//  Created by XD-WangLong on 2/13/14.
//  Copyright (c) 2014 XD-WangLong. All rights reserved.
//

#import "LoginViewController.h"
#import "XDTabBarViewController.h"
#import "FetterViewController.h"
#import "RegisterViewController.h"
#import "WBParams.h"
#import "ResultViewController.h"
#import "DBManager.h"

#define kRedirectURL  @"https://api.weibo.com/oauth2/default.html"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.titleLabel.text =@"注册支付";
}


- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
//    [_timer setFireDate:[NSDate distantFuture]];
}
    

-(void)handleSingleTapFrom{
    [userNameField resignFirstResponder];
    [codeWordField resignFirstResponder];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    [self initRegisterButton];
    
    //注册腾讯信息
//    _tencentOAuth = [[TencentOAuth alloc] initWithAppId:QQAPPID andDelegate:self];
//    //    _tencentOAuth.redirectURI = @"www.qq.com";
//    _permissions = [NSArray arrayWithObjects:@"all", nil];
//    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [rightButton setTitle:@"注册" forState:UIControlStateNormal];
//    [rightButton setTintColor:[UIColor whiteColor]];
//    [rightButton setFrame:CGRectMake(520/2.0f, 13+aHeight, 60, 25)];
//    rightButton.titleLabel.font = [UIFont systemFontOfSize:18];
    //[rightButton addTarget:self action:@selector(goToRegister) forControlEvents:UIControlEventTouchUpInside];
    
    //[self.navigationBarView addSubview:rightButton];

    isFirst = YES;
    orderModel = [[OrderModel alloc] init];

    mScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - UI_STATUS_BAR_HEIGHT - UI_STATUS_BAR_HEIGHT)];
    mScrollView.contentSize=CGSizeMake(UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT -UI_STATUS_BAR_HEIGHT );
    mScrollView.scrollEnabled =YES;
    mScrollView.delegate = self;
    mScrollView.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:mScrollView];
    
  
    UITapGestureRecognizer* singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    [mScrollView addGestureRecognizer:singleRecognizer];
    
    
    
    
//    UIImageView *bgImageView=creatXRImageView(CGRectMake(0, 44, UI_SCREEN_WIDTH , 88), [UIImage imageNamed:@"backGround.png"]);
//    bgImageView.backgroundColor=[UIColor whiteColor];
//    bgImageView.userInteractionEnabled = YES;
//    [mScrollView addSubview:bgImageView];
//
//    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(8,14, 15, 18)];
//    imageView.backgroundColor=[UIColor whiteColor];
//    imageView.image =[UIImage imageNamed:@"useName.png"];
//    [bgImageView addSubview:imageView];
    
    
    userNameField =creatXRTextField(@"请输入手机号码", CGRectMake(15,17,290,44));
    userNameField.delegate=self;
    userNameField.returnKeyType = UIReturnKeyDone;
    userNameField.layer.borderWidth = .5f;
    userNameField.layer.borderColor = RGBA(118, 75, 217, 1).CGColor;
    [mScrollView addSubview:userNameField];
    
    
//    UIImageView *lineImageView=[[UIImageView alloc]initWithFrame:CGRectMake(32, VIEW_POINT_MAX_Y(userNameField)-2, UI_SCREEN_WIDTH -32, 1)];
//    //    lineImageView.image =[UIImage imageNamed:@"line.png"];
//    lineImageView.backgroundColor =UIColorFromRGB(0xdfdfdf);
//    [bgImageView addSubview:lineImageView];
//    
//    
//    UIImageView *passImageView=[[UIImageView alloc]initWithFrame:CGRectMake(8, 57, 15, 18)];
//    [passImageView setBackgroundColor:[UIColor clearColor]];
//    [passImageView setImage:[UIImage imageNamed:@"passdWord"]];
//    imageView.userInteractionEnabled = YES;
//    [bgImageView addSubview:passImageView];
    
    
    codeWordField =creatXRTextField(@"请输入验证码", CGRectMake(15,75, 160, 44));
    codeWordField.layer.borderWidth = .5f;
    codeWordField.layer.borderColor = RGBA(118, 75, 217, 1).CGColor;
    codeWordField.delegate = self;
    codeWordField.returnKeyType = UIReturnKeyDone;
    [mScrollView addSubview:codeWordField];
    
    codeBtn = [XDTools getAButtonForView:mScrollView frame:CGRectMake(180, 75, 125, 44) nomalTitle:@"获取验证码" hlTitle:@"获取验证码" titleColor:[UIColor whiteColor] bgColor:RGBA(241, 141, 0, 1) nbgImage:nil hbgImage:nil action:@selector(getCode) target:self buttonTpye:UIButtonTypeCustom];
    codeBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
//    UILabel *forgetLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, bgImageView.frame.origin.y+bgImageView.frame.size.height+10, 60, 15)];
//    forgetLabel.text = @"忘记密码?";
//    forgetLabel.textColor = [UIColor redColor];
//    forgetLabel.font = [UIFont systemFontOfSize:13.0];
//    forgetLabel.backgroundColor = [UIColor clearColor];
//    
//    [mScrollView addSubview:forgetLabel];
//    
//    
//    UIButton *forgetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    
//    forgetBtn.frame = CGRectMake(250-5, forgetLabel.frame.origin.y, 65, 20);
//    
//    [forgetBtn addTarget:self action:@selector(forgetCode:) forControlEvents:UIControlEventTouchUpInside];
//    
//    [mScrollView addSubview:forgetBtn];
    
    
    
    UIButton *bnt=creatXRButton(CGRectMake(15, 150, UI_SCREEN_WIDTH -30, 44),@"支付",nil,nil);
    [bnt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    bnt.tag =UIBUTTON_TAG;
    [bnt setBackgroundColor:RGBA(241, 141, 0, 1)];
    [bnt setTintColor:[UIColor whiteColor]];
    [bnt addTarget:self action:@selector(zhifu) forControlEvents:UIControlEventTouchUpInside];
    bnt.layer.masksToBounds = YES;
    bnt.layer.cornerRadius = 3;
    [mScrollView addSubview:bnt];
    
    UILabel * label = [XDTools addAlabelForAView:mScrollView withText:@"点击登录即表示您已阅读《喵贷软件许可及服务协议》" frame:CGRectMake(15,199,UI_SCREEN_WIDTH -30, 35) font:[UIFont systemFontOfSize:14] textColor:nil];
    label.numberOfLines = 2;
    label.attributedText = [XDTools getAcolorfulStringWithText1:@"《喵贷软件许可及服务协议》" Color1:RGBA(245, 170, 87, 1) Font1: [UIFont systemFontOfSize:14] Text2:@"点击登录即表示您已阅读" Color2:RGBA(93, 93, 93, 1) Font2: [UIFont systemFontOfSize:14] AllText:label.text];

    [self ruku];
}

- (void)getCode{
    second = 60;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeCodeBtn) userInfo:nil repeats:YES];
    
    codeBtn.userInteractionEnabled = NO;

    [self getData];
}

- (void)changeCodeBtn{
    second--;
    [codeBtn setTitle:[NSString stringWithFormat:@"重新发送(%d)",second] forState:UIControlStateNormal];
    [codeBtn setBackgroundColor:RGBA(245, 170, 87, 1)];
    
    if (!second) {
        [codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        codeBtn.userInteractionEnabled = YES;
        [_timer setFireDate:[NSDate distantFuture]];
        [codeBtn setBackgroundColor:RGBA(241, 141, 0, 1)];
    }
}

//- (void)getLoginParms:(NSDictionary *)parms{
//
//    
//    __weak ASIFormDataRequest *request = [XDTools postRequestWithDict:parms API:API_LOGIN];
//    
//    [request setCompletionBlock:^{
//        [XDTools hideProgress:self.view];
//        
//        NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
//        DDLOG(@"temdic == %@",tempDic);
//        
//        if([[tempDic objectForKey:@"result"]intValue] == 1){
//            
//            NSDictionary *subDict=[tempDic objectForKey:@"userinfo"];
//            NSUserDefaults *userDef=[NSUserDefaults standardUserDefaults];
//            //存登录信息
//          
//            [userDef setObject:subDict forKey:kMMyUserInfo];
//            [userDef synchronize];
//            
//            [XDTools showTips:@"登录成功" toView:self.contentView];
//            [self backPrePage];
//           
//            
//        }else{
//            
//            [XDTools showTips:[tempDic objectForKey:@"msg"] toView:self.view];
//        }
//        
//    }];
//    
//    [request setFailedBlock:^{
//        
//        [XDTools hideProgress:self.view];
//        NSError *error = [request error];
//        DDLOG(@"error=%@",error);
//    }];
//    
//    [XDTools showProgress:self.view];
//    [request startAsynchronous];
//    
//}

- (void)getData{
    if ([XDTools NetworkReachable]){
        NSDictionary * dic = @{@"phone":userNameField.text};

        NSMutableDictionary *muDict = [[NSMutableDictionary alloc] initWithDictionary:dic];
        NSMutableString *params = [[NSMutableString alloc] init];
        for (id keys in muDict) {
            [params appendFormat:@"&%@=%@",keys,[muDict objectForKey:keys]];
        }
        //        NSRange rang = [params rangeOfString:@"?"];
        //        if(rang.location == NSNotFound){
        //            NSRange rangAnd = [params rangeOfString:@"&"];
        //            [params replaceCharactersInRange:rangAnd withString:@"?"];
        //        }

        NSString *urlStr = [NSString stringWithFormat:@"http://www.xindaoapp.com/maodai/plugins/API.v1.0/?&a=category&m=phone_pay%@",params];
        urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        DDLOG(@"urlStr:%@", urlStr);
        NSURL *url = [NSURL URLWithString:urlStr];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setTimeOutSeconds:12];

        ASIHTTPRequest * mrequest = request;

        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            NSString *responseString = [mrequest responseString];
            DDLOG(@"zhifu responseString:%@", responseString);

            NSDictionary * tempDic = [XDTools JSonFromString:[mrequest responseString]];

            if (![tempDic[@"result"] intValue]) {
                [self jiaoyan];
            }else{
                [XDTools showTips:@"您的手机号已参与过本次活动" toView:self.view];
            }

        }];

        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
            NSLog(@"%@",mrequest.error.description);
        }];
        [request startAsynchronous];
        [XDTools showProgress:self.contentView];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
    
}


-(void)loginButtonClick:(UIButton *)button{


    ResultViewController * result = [[ResultViewController alloc] init];
    result.code = @"http://www.xindao.com";
    [self.navigationController pushViewController:result animated:YES];
//    if(button.tag == UIBUTTON_TAG){
//        if([userNameField.text isEqualToString:@""]||[codeWordField.text isEqualToString:@""]){
//            [XDTools showTips:@"信息不完整" toView:self.view];
//            
//        }else{
//            NSDictionary *dic = @{@"userName":userNameField.text,
//                                  @"password":[codeWordField.text MD5Hash],
//                                  @"securitySignal":@"3765673"};
//            [self getLoginParms:dic];
//        }
//        
//    }

    
}

- (void)ruku{

    if ([XDTools NetworkReachable]){
        //        NSString *body = [NSString stringWithFormat:@" <ActivityList xmlns=\"http://tempuri.org/\">"
        //                          "</ActivityList>"];
        NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval a=[dat timeIntervalSince1970]*1000;
        timeString = [NSString stringWithFormat:@"%f", a];
        
        orderModel.payNum = _payAmt;
        orderModel.orderTime = timeString;


        NSDictionary * dic = @{@"merId":@"1000002084",
                               @"merOrderNo": timeString,         //商户订单号
                               @"merUserId": @"test",          //商户ID
                               @"notifyUrl": @"http://124.193.184.94",         ////异步回调地址
                               @"opCode": @"33A10H",             //交易码  入库33A10H
                               @"orderDesc": @"0123",          //描述
                               @"orderType":@"",
                               @"payAmt": _payAmt,
                               @"productId":@"100006",
                               @"returnUrl":@"http://124.193.184.94",
                               @"userId":@""};             

        NSArray * arr = [NSArray arrayWithObjects:@"merId",@"merOrderNo",@"merUserId",@"notifyUrl",@"opCode",@"orderDesc",@"orderType",@"payAmt",@"productId",@"returnUrl",@"userId", nil];


        __weak ASIHTTPRequest * request = [XDTools getRequestWithDict:dic array:arr];
        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            NSString *responseString = [request responseString];
            DDLOG(@"responseString:%@", responseString);

            if ([responseString rangeOfString:@"000000"].length == 6) {
                //保存paytoken
                NSRange range = [responseString rangeOfString:@"orderNo="];
                orderNo = [responseString substringWithRange:NSMakeRange(range.location + range.length, 20)];
            }else{
                [XDTools showTips:@"订单入库失败" toView:self.view];
            }

            
        }];

        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
            NSLog(@"%@",request.error.description);
        }];
        [request startAsynchronous];
        [XDTools showProgress:self.contentView];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
}


- (void)jiaoyan{
    if ([XDTools NetworkReachable]){
            //校验

        orderModel.userID = userNameField.text;

            NSDictionary * dic = @{@"cpuSerial":@"cpu12323232",
                                   @"idNo":@"",                         //身份证
                                   @"imei":@"imei32323232",
                                   @"imsi":@"imsi123333",
                                   @"macAddr":@"mac123",
                                   @"merId":@"1000002084",
                                   @"merVerifyCodeFlag": @"1",          //商户ID
                                   @"opCode": @"28AC0H",             //交易码  入库33A10H                 1
                                   @"orderNo": orderNo,          //订单号                                 1
                                   @"userId":userNameField.text,
                                   @"userIp":@"192.168.1.2"};             //支付金额

            DDLOG(@"dic ::::::  %@",dic);

            NSArray * arr = [NSArray arrayWithObjects:@"cpuSerial",@"idNo",@"imei",@"imsi",@"macAddr",@"merId",@"merVerifyCodeFlag",@"opCode",@"orderNo",@"userId",@"userIp", nil];


            __weak ASIHTTPRequest * request = [XDTools getRequestWithDict:dic array:arr];
            [request setCompletionBlock:^{
                [XDTools hideProgress:self.contentView];
                NSString *responseString = [request responseString];

                DDLOG(@"jiaoyan  responseString:%@", responseString);
                if ([responseString rangeOfString:@"000000"].length == 6) {
                    //保存paytoken
                    NSRange range = [responseString rangeOfString:@"payToken="];
                    payToken = [responseString substringWithRange:NSMakeRange(range.location + range.length, 36)];
                    if (!isFirst) {
                        [self secondGetCode];
                    }
                }else{
                    [XDTools showTips:@"风险校验失败" toView:self.view];
                }

                isFirst = NO;
            }];

            [request setFailedBlock:^{
                [XDTools hideProgress:self.contentView];
                NSLog(@"%@",request.error.description);
            }];
            [request startAsynchronous];
        [XDTools showProgress:self.contentView showText:@"正在进行风控校验"];


    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
}

- (void)secondGetCode{
    if ([XDTools NetworkReachable]){
        //再次获取验证码
        NSDictionary * dic = @{@"merId":@"1000002084",
                               @"opCode": @"64A007",             //交易码  入库33A10H                 1
                               @"payToken": payToken};          //校验时获取                                 1


        NSArray * arr = [NSArray arrayWithObjects:@"merId",@"opCode",@"payToken", nil];


        __weak ASIHTTPRequest * request = [XDTools getRequestWithDict:dic array:arr];
        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            NSString *responseString = [request responseString];

            DDLOG(@"responseString:%@", responseString);

            isFirst = NO;
        }];

        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
            NSLog(@"%@",request.error.description);
        }];
        [request startAsynchronous];
        [XDTools showProgress:self.contentView];

    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }


}

- (void)zhifu{
    if ([XDTools NetworkReachable]){
        //        NSString *body = [NSString stringWithFormat:@" <ActivityList xmlns=\"http://tempuri.org/\">"
        //                          "</ActivityList>"];

        if (!payToken.length) {
            [XDTools showTips:@"请先获取验证码" toView:self.view];
            return;
        }

        if (!codeWordField.text.length) {
            [XDTools showTips:@"请输入验证码" toView:self.view];
            return;
        }


        NSDictionary * dic = @{@"authCode":codeWordField.text,
                               @"merId":@"1000002084",
                               @"opCode": @"34AC0H",             //交易码  入库33A10H                 1
                               @"payToken": payToken};             //

        NSArray * arr = [NSArray arrayWithObjects:@"authCode",@"merId",@"opCode",@"payToken", nil];


        __weak ASIHTTPRequest * request = [XDTools getRequestWithDict:dic array:arr];
        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            NSString *responseString = [request responseString];
            DDLOG(@"zhifu responseString:%@", responseString);

            if ([responseString rangeOfString:@"000000"].length == 6) {
                [self upMyOrder];
                NSArray * arr = [NSArray arrayWithObject:userNameField.text];
                [[NSUserDefaults standardUserDefaults] setObject:arr forKey:kMMyUserInfo];

                orderModel.orderStr = [NSString stringWithFormat:@"%@%@%@",orderModel.orderTime,orderModel.userID,orderModel.payNum];

                [[DBManager shareManager] insertData:orderModel];
            }else{
                [XDTools showTips:@"信用支付失败" toView:self.view];
            }

        }];

        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
            NSLog(@"%@",request.error.description);
        }];
        [request startAsynchronous];
        [XDTools showProgress:self.contentView];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
}

- (void)upMyOrder{
    if ([XDTools NetworkReachable]){
        codeStr = [NSString stringWithFormat:@"%@|%@|%@",_payAmt,timeString,userNameField.text];

        NSDictionary * dic = @{@"phone":codeStr};

        NSMutableDictionary *muDict = [[NSMutableDictionary alloc] initWithDictionary:dic];
        NSMutableString *params = [[NSMutableString alloc] init];
        for (id keys in muDict) {
            [params appendFormat:@"&%@=%@",keys,[muDict objectForKey:keys]];
        }
        //        NSRange rang = [params rangeOfString:@"?"];
        //        if(rang.location == NSNotFound){
        //            NSRange rangAnd = [params rangeOfString:@"&"];
        //            [params replaceCharactersInRange:rangAnd withString:@"?"];
        //        }

        NSString *urlStr = [NSString stringWithFormat:@"http://xindaoapp.com/maodai/plugins/API.v1.0/?&a=category&m=order_insert%@",params];
        urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        DDLOG(@"urlStr:%@", urlStr);
        NSURL *url = [NSURL URLWithString:urlStr];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setTimeOutSeconds:12];

        ASIHTTPRequest * mrequest = request;

        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            NSString *responseString = [mrequest responseString];
            DDLOG(@"upOrder responseString:%@", responseString);
//           NSDictionary * dic = [XDTools JSonFromString:responseString];
//            if ([dic[@"result"] intValue] == 1) {
                ResultViewController * result = [[ResultViewController alloc] init];
                result.code = codeStr;
                [self.navigationController pushViewController:result animated:YES];
//            }


        }];

        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
            NSLog(@"%@",mrequest.error.description);
        }];
        [request startAsynchronous];
        [XDTools showProgress:self.contentView];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }

}

- (void)backFirstPage{

}







#pragma mark  重写父类的方法和键盘消失
- (void)backPrePage
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"wblogin" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [UIView animateWithDuration:0.6 animations:^{
        mScrollView.contentOffset = CGPointMake(0, 0);
    }];
    [self keyBoardHide];
    
}
-(void)keyBoardHide{
    [userNameField resignFirstResponder];
    [codeWordField resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)initRegisterButton
{
    UIButton *registerButton= [UIButton buttonWithType:UIButtonTypeCustom];
    //_leftBtn.frame = LEFTBUTTONFRAME(aHeight);
    CGFloat backForIOS6 =0;
    if (IOS7) {
        backForIOS6 = 20;
    }
    registerButton.frame = CGRectMake((UI_SCREEN_WIDTH - 44-10),backForIOS6, 44, 44);
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [registerButton setTintColor:[UIColor whiteColor]];
    
    [registerButton addTarget:self action:@selector(goToRegister) forControlEvents:UIControlEventTouchUpInside];
    registerButton.adjustsImageWhenHighlighted =NO;
    [self.navigationBarView addSubview:registerButton];
}


#pragma mark - scroller view 注销键盘
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [userNameField resignFirstResponder];
    [codeWordField resignFirstResponder];
}

//去注册
-(void)goToRegister{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
    
}

#pragma mark - TextField Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (!iPhone5) {
        [UIView animateWithDuration:.3 animations:^{
            mScrollView.contentOffset = CGPointMake(0, 30);
        }];
    }
    
    return YES;
}
-(void)qqLoginToservice:(NSString *)openId {
    __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:API_QQLOGIN]];
    [request setPostValue:@"qqlogincheck" forKey:@"action"];
    [request setPostValue:openId forKey:@"conopenid"];
    [request setTimeOutSeconds:12];
    
    [request setRequestCookies:[XDTools productCookies]];
    [request setUseCookiePersistence:NO];
    [request setUserAgentString:@"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0"];
    
    [request setCompletionBlock:^{
        [XDTools hideProgress:self.view];
        
        NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
        DDLOG(@"temdic == %@",tempDic);
        
        if([[tempDic objectForKey:@"result"]intValue] == 1){
            
            NSDictionary *subDict=[tempDic objectForKey:@"userinfo"];
            NSUserDefaults *userDef=[NSUserDefaults standardUserDefaults];
            //存登录信息
            
            
            
            [userDef setObject:subDict forKey:kMMyUserInfo];
            [userDef synchronize];
            
            [XDTools showTips:@"登录成功" toView:self.contentView];
            [self backPrePage];
           
            
        }else if([[tempDic objectForKey:@"result"]intValue] == 101){
            //跳入绑定界面
            FetterViewController *fetterVC = [[FetterViewController alloc] init];
            fetterVC.openId = openId;
            fetterVC.type = @"QQ";
            [self.navigationController pushViewController:fetterVC animated:YES];
        }else{
            [XDTools showProgress:self.view showText:[tempDic objectForKey:@"msg"]];
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                sleep(1.0);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [XDTools hideProgress:self.view];
                });
                
            });
        }
        
    }];
    
    [request setFailedBlock:^{
        
        [XDTools hideProgress:self.view];
    }];
    
    [XDTools showProgress:self.view];
    [request startAsynchronous];
    
}


//新浪微博；；；；；；；；；；；；；；；；；；；

-(void)wbLoginToservice:(NSNotification *)notification {
    WBParams *wbp = notification.object;
    NSString *openId = wbp.wbId;
    __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:API_QQLOGIN]];
    [request setPostValue:@"wblogincheck" forKey:@"action"];
    [request setPostValue:openId forKey:@"sina_uid"];
    [request setPostValue:wbp.wbToken forKey:@"token"];
    [request setPostValue:wbp.expireTime forKey:@"oauth2_expiretime"];
    [request setTimeOutSeconds:12];
    
    [request setRequestCookies:[XDTools productCookies]];
    [request setUseCookiePersistence:NO];
    [request setUserAgentString:@"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0"];
    [request setCompletionBlock:^{
    
        [XDTools hideProgress:self.view];
        
        NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
        DDLOG(@"temdic == %@",tempDic);
        
        if([[tempDic objectForKey:@"result"]intValue] == 1){
            
            NSDictionary *subDict=[tempDic objectForKey:@"userinfo"];
            NSUserDefaults *userDef=[NSUserDefaults standardUserDefaults];
            //存登录信息
         
            
            
            [userDef setObject:subDict forKey:kMMyUserInfo];
            [userDef synchronize];
            
            [XDTools showTips:@"登录成功" toView:self.contentView];
            [self backPrePage];
            
            
        }else if([[tempDic objectForKey:@"result"]intValue] == 101){
            //跳入绑定界面
            FetterViewController *fetterVC = [[FetterViewController alloc] init];
            fetterVC.wbparams = wbp;
            fetterVC.type = @"wb";
            [self.navigationController pushViewController:fetterVC animated:YES];
        }else{
            [XDTools showProgress:self.view showText:[tempDic objectForKey:@"msg"]];
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                sleep(1.0);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [XDTools hideProgress:self.view];
                });
                
            });
        }
        
    }];
    
    [request setFailedBlock:^{
        
        [XDTools hideProgress:self.view];
    }];
    
    [XDTools showProgress:self.view];
    [request startAsynchronous];
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    [UIView animateWithDuration:.3 animations:^{
        mScrollView.contentOffset = CGPointMake(0, 0);
    }];
    return YES;
}


//忘记密码
-(void)forgetCode:(UIButton *)btn{
    if (!IS_NOT_EMPTY(userNameField.text)) {
        [XDTools showTips:@"账号不能为空" toView:self.view];
    }else{
        NSDictionary *dic = @{@"userName": userNameField.text};
        [self forgetCodeParms:dic];
    }
}


//忘记密码 接口

- (void)forgetCodeParms:(NSDictionary *)parms{
    
    __weak  ASIFormDataRequest *request = [XDTools postRequestWithDict:parms API:API_FORGETCODE];
    
    [request setCompletionBlock:^{
        [XDTools hideProgress:self.view];
        
        NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
        if([[tempDic objectForKey:@"result"]intValue] == 1){
            
            [XDTools showTips:@"新密码已发送至手机" toView:self.view];
            
        }else{
            [XDTools showTips:tempDic[@"msg"] toView:self.view];
            
        }
    }];
    
    [request setFailedBlock:^{
        
        [XDTools hideProgress:self.view];
        NSError *error = [request error];
        DDLOG(@"error=%@",error);
    }];
    
    [XDTools showProgress:self.view];
    [request startAsynchronous];
    
}




-(void)viewWillDisappear:(BOOL)animated{
    
}


@end
